package com.rocksdata.mc.business;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.webkit.WebView;

import com.alibaba.fastjson.JSON;
import com.baidu.location.BDLocation;
import com.baidu.location.Poi;
import com.ctz.commonframe.ui.BaseActivity;
import com.ctz.commonframe.ui.WebViewActivity;
import com.ctz.commonframe.utils.Logger;
import com.ctz.commonframe.utils.SharedPrefrencesUtil;
import com.rocksdata.mc.BuildConfig;
import com.rocksdata.mc.business.location.BaiduLocationListener;
import com.rocksdata.mc.business.location.LocationBean;
import com.rocksdata.mc.business.location.LocationInfo;
import com.rocksdata.mc.business.location.LocationManager;
import com.rocksdata.mc.business.location.UnreadManager;
import com.rocksdata.mc.database.ApplicationInfoDBManager;
import com.rocksdata.mc.entity.CampusAppInfo;
import com.rocksdata.mc.entity.TeacherInfo;
import com.rocksdata.mc.entity.UnreadCache;
import com.rocksdata.mc.entity.WidgetInfo;
import com.rocksdata.mc.http.HttpClient;
import com.rocksdata.mc.ui.activity.GesturePasswordUnlockActivity;
import com.rocksdata.mc.SharedPreferencesKey;
import com.rocksdata.mc.database.DatabaseManager;
import com.rocksdata.mc.ui.activity.LoginActivity;
import com.rocksdata.mc.ui.activity.UndevelopedActivity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 陈堂正 on 2017/4/25.
 */

public class CampusAppManager implements BusinessInterface.ICampusAppManager {
    private static final String URL_GET_APP_LIST = BuildConfig.HOST + "/apps";
    private static final String BASE_WEB_CAMPUS_APP_URL = "file:///android_asset/www/index.html";
    private static final String BASE_WEB_OA_APP_URL = "file:///android_asset/oa/index.html";

    private static BusinessInterface.ICampusAppManager instance;
    private ApplicationInfoDBManager manager;

    private CampusAppManager(){
        manager = DatabaseManager.getApplicationInfoDBManager();
    }

    static synchronized BusinessInterface.ICampusAppManager getInstance(){
        if(instance == null)
            instance = new CampusAppManager();
        return instance;
    }

    @Override
    public void syncAppList(final BusinessInterface.OnBusinessResultListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("userID", UserInfoManager.getCurrentUserId());
        map.put("type", UserInfoManager.getCurrentUserType());

        HttpClient.get(URL_GET_APP_LIST, map, CampusAppInfo.class, new HttpClient.OnHttpResultListener<CampusAppInfo>(){

            @Override
            public void onResult(boolean isSuccess, List<CampusAppInfo> data, String msg) {
                if(isSuccess){
                    manager.deleteAllApp();
                    manager.saveAppList(data);
                }
                List<CampusAppInfo> appList = getHomepageApps();
                if (appList == null) {
                    appList = new ArrayList<>();
                    for(CampusAppInfo info: data){
                        if (info.usable() && !appList.contains(info)) {
                            appList.add(info);
                        }
                        if (appList.size() == 11) {
                            break;
                        }
                    }
                    saveHomepageApps(appList);
                }

                listener.onBusinessResult(true, appList, null);
            }
        });
    }

    public static List<String> getHomepageAppCodes () {
        String str = (String) SharedPrefrencesUtil.getAboutUser(UserInfoManager.getCurrentUserId(), SharedPreferencesKey.KEY_HOME_PAGE_APPS, null);
        if (TextUtils.isEmpty(str))
            return null;
        return new ArrayList<>(Arrays.asList(str.split(",")));
    }

    private static void saveHomepageAppCodes (String codes) {
        SharedPrefrencesUtil.putAboutUser(UserInfoManager.getCurrentUserId(), SharedPreferencesKey.KEY_HOME_PAGE_APPS, codes);
    }

    @Override
    public List<CampusAppInfo> getAllAppList(){
        return manager.getAllAppList();
    }

    @Override
    public List<CampusAppInfo> getHomepageApps () {
        List<String> codes = getHomepageAppCodes();
        if (codes == null)
            return null;
        return manager.getAppListByCodes(codes);
    }

    /**
     * 首页应用排序
     *
     * @param list 应用列表
     */
    public static void saveHomepageApps(List<CampusAppInfo> list){
        String result = "";
        for(int i = 0; i < list.size(); i++){
            result += list.get(i).getCode();
            if (i < list.size() - 1)
                result += ",";
        }
        saveHomepageAppCodes(result);
    }

    @Override
    public List<CampusAppInfo> getGesturePasswordApps () {
        List<String> codes = getGesturePasswordAppCodes();
        List<CampusAppInfo> list = manager.getAppListByCodes(codes);
        return list;
    }

    public static void saveGesturePasswordApps(List<CampusAppInfo> list){
        String result = "";
        for(int i = 0; i < list.size(); i++){
            result += list.get(i).getCode();
            if (i < list.size() - 1)
                result += ",";
        }
        saveGesturePasswordAppCodes(result);
    }

    public static void saveGesturePasswordAppCodes(List<String> list){
        String result = "";
        for(int i = 0; i < list.size(); i++){
            result += list.get(i);
            if (i < list.size() - 1)
                result += ",";
        }
        saveGesturePasswordAppCodes(result);
    }

    private static void saveGesturePasswordAppCodes(String codes) {
        SharedPrefrencesUtil.putAboutUser(UserInfoManager.getCurrentUserId(), SharedPreferencesKey.KEY_GESTURE_PASSWORD_APPS, codes);
    }

    public static List<String> getGesturePasswordAppCodes () {
        String str = (String) SharedPrefrencesUtil.getAboutUser(UserInfoManager.getCurrentUserId(), SharedPreferencesKey.KEY_GESTURE_PASSWORD_APPS, null);
        if (TextUtils.isEmpty(str))
            return new ArrayList<>();
        String[] codes = str.split(",");
        if (codes.length == 0)
            return new ArrayList<>();
        return new ArrayList<>(Arrays.asList(codes));
    }


    /**
     * 跳转到子应用
     * @param context 上下文
     * @param info 校园应用信息
     */
    public static void skipToChildApplication(final Context context, final CampusAppInfo info){
        if (!info.usable()) return;
        try {
            String name = info.getName();
            List<String> gesturePasswordAppCodes = getGesturePasswordAppCodes();
            boolean isNeedGesturePassword = gesturePasswordAppCodes.contains(info.getCode());
            String className = null;
            Intent appIntent = new Intent();
            if("搜周边".equals(name)){
                className = "com.ctz.commonframe.ui.WebViewActivity";
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_TITLE, name);
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, "file:///android_asset/oa/index.html#/surrounding_area");
    //            className = "com.rocksdata.mip.ui.activity.SearchNearbyActivity";
            }else if("扫一扫".equals(name)){
                className = "com.rocksdata.mip.ui.activity.QRCodeScanActivity";
            }else if("工资单".equals(name)){
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/salary");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
    //            className = "com.rocksdata.mip.ui.activity.PayrollActivity";
            }else if("课表".equals(name)){
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/timetable");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
    //            className = "com.rocksdata.mip.ui.activity.TimetableActivity";
            }else if("课堂考勤".equals(name)){
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/attendance");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("通讯录".equals(name)){
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/contacts");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
    //            className = "com.rocksdata.mobilecampus.ui.activity.ContactActivity";
            }else if("校园新闻".equals(name)){
                className = "com.rocksdata.mc.ui.activity.NewsActivity";
//            }else if("待办已办".equals(name)){
//                className = "com.rocksdata.mip.ui.activity.OATreatActivity";
            }else if("招生简章".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_TITLE, name);
                String url = "http://koming.zheng.gitee.io/peixian/enrollment/enrollment.html";
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
    //            className = "com.rocksdata.mip.ui.activity.SchoolProfileActivity";
            }else if("校历".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/calendar");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("全景校园".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
//                String url = getWebCampusAppUrl("#/panoramic_campus");
                String url = "http://www.rocksdata.com:2000/3d/";
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_TITLE, name);
//                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, "https://720yun.com/t/56128wOfx4w?from=singlemessage&pano_id=124783");
            }else if("投票".equals(name)){
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/vote_main");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("问卷".equals(name)){
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/questionnaire");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("调休单".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/live_in_lieu");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("教学督导巡视".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/supervisory_inspection");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("干部巡视".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/cadre_inspection");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("班级考评".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/class_evaluations");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("客饭券".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/guest_meal_coupons");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("会议签到".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/meeting_sign_in");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("会议日程".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/meeting_schedule");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("学校概况".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
//                String url = getWebCampusAppUrl("#/intramural_activities");
                String url = "https://s.wcd.im/v/7um1rZ35/";
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_TITLE, name);
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("一卡通".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/one_cartoon");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("招聘信息".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/recruitment_information");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("招生信息".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/students_information");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("就业指导".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/employ_guidance");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("新生资讯".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/info_freshman");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("成绩单".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/academic_report");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("实习日志".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/internship_log");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("听课".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/listen_lesson");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("课时统计".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/class_statistics");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("教学日志".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/teaching_log");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("成长轨迹".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/growth_trajectory");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("安全巡视".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/safety_inspection");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("学生评教".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/student_evaluation");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("职工手册".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/staff_handbook");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("校企合作".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/school_enterprise_cooperation");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("毕业生回访".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/graduate");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("校园邮箱".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/campus_mail");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("教研活动".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/research_activity");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("评学".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/evaluation_learning");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("教学质量".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/teaching_quality");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("任务安排".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/task_arrangement");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("会议室申请".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebOAAppUrl("#/meeting_room_apply");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("用印申请".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebOAAppUrl("#/seal_apply");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("实训室申请".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/training_rooms");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("教师请假".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/teacher_leave");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("调代课申请".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebOAAppUrl("#/belt_lesson");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("文件审批".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebOAAppUrl("#/post_process_apply");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("公文流转".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebOAAppUrl("#/document_flow_apply");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("故障报修".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebOAAppUrl("#/repairs");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("待办已办".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/todo");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("我的申请".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/my_applies2");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("驾驶舱".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/cockpit");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("宿舍考勤".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/attendance_check");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("宿舍考评".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/dormitory_check");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else if("人脸采集".equals(name)) {
                className = "com.ctz.commonframe.ui.WebViewActivity";
                String url = getWebCampusAppUrl("#/face_collect");
                appIntent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
            }else{
                appIntent.setClass(context, UndevelopedActivity.class);
                isNeedGesturePassword = false;
                appIntent.putExtra("app_name", name);
            }

            if(isNeedGesturePassword){
                appIntent.setClass(context, GesturePasswordUnlockActivity.class);
                appIntent.putExtra("className", className);
            }else{
                if (className != null)
                    appIntent.setClassName(context, className);
            }

            context.startActivity(appIntent);


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取web校园应用的url
     *
     * @param router 路由
     * @return 可访问的url
     * @throws UnsupportedEncodingException URLEncoder.encode抛出
     */
    public static String getWebCampusAppUrl(String router) throws UnsupportedEncodingException {
        return BASE_WEB_CAMPUS_APP_URL
                + router
                + "?baseUrl=" + URLEncoder.encode(BuildConfig.HOST, "UTF-8")
                + "&baseURLOA=" + URLEncoder.encode(BuildConfig.OA_INTERFACE_HOST, "UTF-8")
                + "&userType=" + UserInfoManager.getCurrentUserType()
                + "&userId=" + UserInfoManager.getCurrentUserId()
                + "&token=" + UserInfoManager.getAccessToken()
                + "&backClose";
    }

    /**
     * 获取客饭券推送消息进入页面的url
     *
     * @param labour 在职状态，1在职，2外聘
     * @param table 状态，1申请，2审核
     * @param code 客饭券唯一标示
     * @return
     */
    public static String getMealDetailUrl(int labour, int table, String code) throws UnsupportedEncodingException {
        String route = "";
        if (labour == 1) { //在职老师
            if (table == 1) { //详情页
                route = "/GuestMealCouponsDetail/" + code;
            } else if (table == 2) { //审核页
                route = "/audit/" + code;
            } else {
                return null;
            }
        } else if (labour == 2) { //外聘老师
            route = "/GetOutside/" + code;
        } else {
            return null;
        }
        return BASE_WEB_CAMPUS_APP_URL
                + "#/guest_meal_coupons"
                + route
                + "?baseUrl=" + URLEncoder.encode(BuildConfig.HOST, "UTF-8")
                + "&token=" + UserInfoManager.getAccessToken()
                + "&userType=" + UserInfoManager.getCurrentUserType()
                + "&userId=" + UserInfoManager.getCurrentUserId()
                + "&backClose";
    }

    /**
     * 获取web流程应用rul
     *
     * @param router 路由
     * @return 可访问的url
     * @throws UnsupportedOperationException
     */
    public static String getWebOAAppUrl(String router) throws UnsupportedEncodingException {
        return BASE_WEB_CAMPUS_APP_URL
                + router
                + "?baseUrl=" + URLEncoder.encode(BuildConfig.HOST, "UTF-8")
                + "&baseURLOA=" + URLEncoder.encode(BuildConfig.OA_INTERFACE_HOST, "UTF-8")
                + "&userId=" + UserInfoManager.getCurrentUserId()
                + "&userName=" + UserInfoManager.getCurrentName()
                + "&departmentCode=" + UserInfoManager.getDepartmentCode()
                + "&departmentName=" + UserInfoManager.getDepartmentName()
                + "&token=" + UserInfoManager.getAccessToken()
                + "&backClose";
    }

    public static String getOADetailUrl(String processName, String instanceId, String taskName, boolean isMyApply) throws UnsupportedEncodingException{
        String tag = isMyApply ? "applicant" : "auditor";
        String head = isMyApply ? "#/my_applies2/detail/" : "#/todo/detail/";
        String tName = isMyApply ? "" : ("/" + URLEncoder.encode(taskName, "UTF-8"));
        return BASE_WEB_CAMPUS_APP_URL
                + head
                + UserInfoManager.getCurrentUserId()
                + "/" + instanceId
                + tName
                + "?my=" + tag
                + "&name=" + URLEncoder.encode(processName, "UTF-8")
                + "&baseUrl=" + URLEncoder.encode(BuildConfig.HOST, "UTF-8")
                + "&baseURLOA=" + URLEncoder.encode(BuildConfig.OA_INTERFACE_HOST, "UTF-8")
                + "&userId=" + UserInfoManager.getCurrentUserId()
                + "&userName=" + URLEncoder.encode(UserInfoManager.getCurrentName(), "UTF-8")
                + "&departmentCode=" + UserInfoManager.getDepartmentCode()
                + "&departmentName=" + URLEncoder.encode(UserInfoManager.getDepartmentName(), "UTF-8")
                + "&token=" + UserInfoManager.getAccessToken()
                + "&backClose";

    }

//    private static String getOAApplyUrl(String FlowName, String FlowId){
//        return BuildConfig.OA_PAGE_HOST + "/WapApproval/PeiXian/" + FlowName + "/Step_1.aspx"
//                + "?FlowID=" + FlowId
//                + "&Action=New"
//                + "&key=" + System.currentTimeMillis()
//                + "&uid=" + UserInfoManager.getCurrentUserId()
//                + "&pwd=" + UserInfoManager.getOAPassword();
//    }

    /**
     * 获取用户卡片列表
     *
     * @return 卡片列表
     */
    public static List<WidgetInfo> getUserWidgetList(){
        List<WidgetInfo> list = new ArrayList<>();
        String str = (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_USER_WIDGET, "");
        if(TextUtils.isEmpty(str)){
            list.add(new WidgetInfo(WidgetInfo.TYPE_BANNER));
            list.add(new WidgetInfo(WidgetInfo.TYPE_APP));
            list.add(new WidgetInfo(WidgetInfo.TYPE_ANNOUNCEMENT));
            list.add(new WidgetInfo(WidgetInfo.TYPE_TIMETABLE));
            list.add(new WidgetInfo(WidgetInfo.TYPE_NEWS));
            list.add(new WidgetInfo(WidgetInfo.TYPE_WIDGET_MANAGER));

            StringBuilder typeStr = new StringBuilder();
            for(int i = 0; i < list.size(); i++){
                typeStr.append(list.get(i).type);
                if(i < list.size() - 1){
                    typeStr.append(",");
                }
            }
            SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_USER_WIDGET, typeStr.toString());
        }else{
            String[] types = str.split(",");
            for(String type: types){
                list.add(new WidgetInfo(Integer.parseInt(type)));
            }
        }
        return list;
    }

    /**
     * 获取游客卡片列表
     *
     * @return 卡片列表
     */
    public static List<WidgetInfo> getVisitorWidgetList(){
        List<WidgetInfo> list = new ArrayList<>();
        String str = (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_VISITOR_WIDGET, "");
        if(TextUtils.isEmpty(str)){

            list.add(new WidgetInfo(WidgetInfo.TYPE_BANNER));
            list.add(new WidgetInfo(WidgetInfo.TYPE_APP));
            list.add(new WidgetInfo(WidgetInfo.TYPE_NEWS));
            list.add(new WidgetInfo(WidgetInfo.TYPE_WIDGET_MANAGER));

            StringBuilder typeStr = new StringBuilder();
            for(int i = 0; i < list.size(); i++){
                typeStr.append(list.get(i).type);
                if(i < list.size() - 1){
                    typeStr.append(",");
                }
            }
            SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_VISITOR_WIDGET, typeStr.toString());
        }else{
            String[] types = str.split(",");
            for(String type: types){
                list.add(new WidgetInfo(Integer.parseInt(type)));
            }
        }
        return list;
    }
}
